package com.scm.servlet.vender;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.scm.dao.VenderDao;
import com.scm.model.Vender;
import com.scm.util.StringUtil;

@WebServlet("/main/pomain/addvender")
public class AddVenderServlet extends HttpServlet {
	private static final long serialVersionUID = -864112139352508687L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String vendercode = request.getParameter("vendercode").trim();
		String name = request.getParameter("name").trim();
		String password = request.getParameter("password").trim();
		if(StringUtil.isEmpty(password)) {
			password=vendercode;
		}
		String contactor = request.getParameter("contactor").trim();
		String address = request.getParameter("address").trim();
		String postcode = request.getParameter("postcode").trim();
		String tel = request.getParameter("tel").trim();
		String fax = request.getParameter("fax").trim();
		String createdate = request.getParameter("createdate").trim();
		Vender vender = new Vender();
		vender.setVenderCode(vendercode);
		vender.setName(name);
		vender.setPassword(password);
		vender.setPostCode(postcode);
		vender.setAddress(address);
		vender.setContactor(contactor);
		vender.setTel(tel);
		vender.setFax(fax);
		vender.setCreateDate(createdate);
		VenderDao venderdao = new VenderDao();
		try {
			int num = venderdao.query(vendercode);
			PrintWriter pw = response.getWriter();
			if(num==-1) {
					venderdao.add(vender);
					pw.print("添加成功");
					pw.flush();
					pw.close();
			}else {
				pw.print("供应商编码重复");
				pw.flush();
				pw.close();
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
}
